package com.example.onion.hxchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.databean.InfoBean;
import com.example.onion.hxchat.room.entity.Info;
import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 适配器，你们应该不用用到我的适配器不多写了吧
 */
public class MyInfoAdapter extends RecyclerView.Adapter<MyInfoAdapter.ViewHolder> {

    private List<Info> mList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;
    private DataGetterListener mDataGetterListener;
    private Context mContext;

    public MyInfoAdapter(Context context, List<Info> mList) {
        this.mContext = context;
        this.mList = mList;
    }

    public MyInfoAdapter()
    {

    }

    public void setmList(List<Info> mList) {
        this.mList = mList;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView infoImg;      //消息页面子项的用户头像
        TextView infoUserName;  //消息页面显示的用户名称
        TextView infoContent;   //显示最后一条信息（理论上)

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            infoImg = itemView.findViewById(R.id.info_img_wc);
            infoUserName = itemView.findViewById(R.id.info_name_wc);
            infoContent = itemView.findViewById(R.id.info_last_content_wc);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.info_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        Info infoBean = mList.get(position);
        holder.infoImg.setImageResource(R.drawable.like);
        holder.infoUserName.setText(infoBean.getUserName());
        holder.infoContent.setText(infoBean.getLastInfo());

        //适配器只负责得到数据吧，跳转页面由从适配器获取到数据的Activity或者Fragment去选择跳转到哪个页面
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(position);
                    //通过recyclerview的position指定当前行的数据，回调给调用方
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
        if(mDataGetterListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Info infoBean = mList.get(position);
                    mDataGetterListener.getItemData(infoBean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);

    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(Info infoBean);//将本页面的获得的infoBean数据通过接口回调给调用方
    }

    public void setDataGetterListener(DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }
}
